package exam.aopLogger.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.test.datatemp.IoUtils;
import exam.aopLogger.domain.LogContent;
import exam.aopLogger.domain.LogRecord;
import exam.aopLogger.service.LogContentService;
import exam.aopLogger.service.LogRecordService;
import exam.common.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.io.File;

/**
 * @author Xiongx
 * @version 1.0
 * @date 2021/4/11 15:38
 * @since JDK 1.8
 */
@RestController
@Api(tags = {"日志模块"})
@RequestMapping("log")
public class LogController {

    @Autowired
    private LogRecordService recordService;

    @Autowired
    private LogContentService logContentService;


    private static final String devPATH=System.getProperty("user.dir");

    public static void main(String[] args) {
        File file=new File(devPATH+File.separator+"logs"+File.separator+"spring.log");
        String str= IoUtils.txt2String(file);
        System.out.println(str);
    }


    @GetMapping("sysloginfo")
    @ApiOperation("系统日志详细信息")
    public JSONObject getSysLogController(){
        File file=new File(devPATH+File.separator+"logs"+File.separator+"spring.log");
        String str= IoUtils.txt2String(file);
        return Result.successObject(str);
    }

    @PostMapping("list")
    @ApiModelProperty("分页列表")
    public JSONObject getLogRecordController(@ApiParam("页码")@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                                             @ApiParam("条数")@RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize,
                                             @ApiParam("关键字")@RequestParam(value = "keyword",required = false)String keyword){
        LambdaQueryWrapper<LogRecord> lambdaQueryWrapper= Wrappers.lambdaQuery();
        if(StringUtils.hasText(keyword)){
            lambdaQueryWrapper.like(LogRecord::getServerName,keyword);
            lambdaQueryWrapper.or().like(LogRecord::getRestAddr,keyword);
            lambdaQueryWrapper.or().like(LogRecord::getRestMethod,keyword);
        }
        lambdaQueryWrapper.orderByDesc(LogRecord::getCreateTime);
        IPage<LogRecord> logRecordIPage=recordService.page(new Page(pageNum,pageSize),lambdaQueryWrapper);
        return Result.successObject(logRecordIPage.getTotal(),
                logRecordIPage.getRecords());
    }

    @GetMapping("getinfo")
    public JSONObject getLonContentController(@ApiParam("笔记id")@RequestParam("id") String id){
        if(!StringUtils.hasText(id)){
            return Result.errorObject("id信息不得为空");
        }
        LogRecord logRecord=recordService.getById(id);
        if(logRecord==null){
            return Result.errorObject("数据已过期");
        }
        LogContent logContent=logContentService.getOne(Wrappers.<LogContent>lambdaQuery()
        .eq(LogContent::getLogId,id));
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("serverName",logRecord.getServerName());
        jsonObject.put("argnote",logContent.getArgJson());
        jsonObject.put("resultObj",logContent.getContent());
        return Result.successObject(jsonObject);
    }
}
